#include "img_3dinfection.h"

img_3DInfection::img_3DInfection(vtkRenderWindow *_window, QObject *parent)
    : QObject{parent}
    , window(_window)
    , renderer(vtkSmartPointer<vtkRenderer>::New())
    , skinExtractor(vtkSmartPointer<vtkContourFilter>::New())
    , skinNormals(vtkSmartPointer<vtkPolyDataNormals>::New())
    , skinMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
    , skin(vtkSmartPointer<vtkActor>::New())
    , outlineData(vtkSmartPointer<vtkOutlineFilter>::New())
    , mapOutline(vtkSmartPointer<vtkPolyDataMapper>::New())
    , outline(vtkSmartPointer<vtkActor>::New())
    , camera(vtkSmartPointer<vtkCamera>::New())
    , axes(vtkSmartPointer<vtkAxesActor>::New())
{
}

void img_3DInfection::set3DInfection(vtkImageData *infection)
{
    this->skinExtractor->SetInputData(infection);
    this->skinExtractor->SetValue(1, 1);
    //---------------------------------------------------------------------------
    //病灶数据分析
    this->skinNormals->SetInputConnection(this->skinExtractor->GetOutputPort());
    this->skinNormals->SetFeatureAngle(60.0);
    this->skinMapper->SetInputConnection(this->skinNormals->GetOutputPort());
    this->skinMapper->ScalarVisibilityOff();
    //---------------------------------------------------------------------------
    //病灶显示
    this->skin->SetMapper(this->skinMapper);
    this->skin->GetProperty()->SetColor(1, 0, 0);
    //---------------------------------------------------------------------------
    //外侧包围盒
    this->outlineData->SetInputData(infection);
    this->mapOutline->SetInputConnection(this->outlineData->GetOutputPort());
    this->outline->SetMapper(this->mapOutline);
    this->outline->GetProperty()->SetColor(1, 1, 1);
    //---------------------------------------------------------------------------
    //坐标轴
    this->axes->SetXAxisLabelText("x");
    this->axes->SetYAxisLabelText("y");
    this->axes->SetZAxisLabelText("z");
    this->axes->SetTotalLength(infection->GetDimensions()[0]*infection->GetSpacing()[0] + 100,
                               infection->GetDimensions()[1]*infection->GetSpacing()[1] + 100,
                               infection->GetDimensions()[2]*infection->GetSpacing()[2] + 100);
    this->axes->SetVisibility(true);
    //---------------------------------------------------------------------------
    //相机视角
    this->camera->SetViewUp (0, 0, 0);
    this->camera->SetPosition (
        infection->GetDimensions()[0]*infection->GetSpacing()[0] / 2,
        infection->GetDimensions()[1]*infection->GetSpacing()[1] / 2, 1200);
    this->camera->SetFocalPoint (
        infection->GetDimensions()[0]*infection->GetSpacing()[0] / 2,
        infection->GetDimensions()[1]*infection->GetSpacing()[1] / 2,
        infection->GetDimensions()[2]*infection->GetSpacing()[2] / 2
    );
    //---------------------------------------------------------------------------
    this->renderer->AddActor(this->outline);
    this->renderer->AddActor(this->skin);
    this->renderer->AddActor(this->axes);
    this->renderer->SetActiveCamera(this->camera);
    this->renderer->ResetCameraClippingRange();
    this->renderer->SetBackground(0.1, 0.1, 0.1);
    this->renderer->ResetCameraClippingRange ();
    this->window->AddRenderer(renderer);
    this->window->Render();
    seted = true;
}

void img_3DInfection::clear()
{
    this->window->RemoveRenderer(renderer);
    this->window->Render();
    seted = false;
}

bool img_3DInfection::isSet()
{
    return seted;
}
